package shuashuami.hb.com.avtivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;

import com.scrat.app.selectorlibrary.ImageSelector;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import shuashuami.hb.com.adapter.ComplaintAdatper;
import shuashuami.hb.com.hbclient.R;
import shuashuami.hb.com.hdlibrary.util.ToastUtil;
import shuashuami.hb.com.http.LoginHttp;
import shuashuami.hb.com.http.QuerenDingdanHttp;

public class CQuerenshouhuobingpingjiaActivity extends AbActivity {

    private GridView gridView;
    private ComplaintAdatper complaintAdatper;
    private List<String> imgs;
    private Button btnSubmit;

    private static final int REQUEST_CODE_SELECT_IMG = 1;
    private static final int MAX_SELECT_COUNT = 9;


    private String taskId;

    @Override
    protected void initUI() {
        setAbContentView(R.layout.activity_querenshouhuobingxiangshou);
        gridView = (GridView) findViewById(R.id.gv_complaint);
        btnSubmit = (Button) findViewById(R.id.btn_complaint_submit);
    }

    @Override
    protected void initData() {
        setTitleBar("确认收货");
        Bundle bundle = this.getIntent().getExtras();
        //接收name值
        taskId = bundle.getString("id");
        setLeftView();
        initAdapter();
    }

    private void initAdapter() {
        imgs = new ArrayList<>();
        imgs.add(R.mipmap.ssm_icon_127 + "");
        complaintAdatper = new ComplaintAdatper(this, imgs);
        gridView.setAdapter(complaintAdatper);
    }

    @Override
    protected void initListener() {
        setLeftViewListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position == imgs.size() - 1) {
                    if (imgs.size() <= 9) {
                        ImageSelector.show(CQuerenshouhuobingpingjiaActivity.this, REQUEST_CODE_SELECT_IMG, MAX_SELECT_COUNT);
                    }
                }
            }
        });
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submit();
            }
        });
    }

    private void showContent(Intent data) {
        List<String> paths = ImageSelector.getImagePaths(data);
        if (!paths.isEmpty()) {
            for (int i = 0; i < paths.size(); i++) {
                imgs.add(0, paths.get(i));
            }
            complaintAdatper.notifyDataSetChanged();
            return;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE_SELECT_IMG) {
            showContent(data);
            return;
        }

        super.onActivityResult(requestCode, resultCode, data);
    }

    final Handler handler = new Handler() {

        public void handleMessage(Message msg) {         // handle message
            switch (msg.what) {

                case 2:
                    String result = msg.obj.toString();
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        if (jsonObject.getInt("status") == 203) {
                            LoginHttp loginHttp = new LoginHttp(CQuerenshouhuobingpingjiaActivity.this);
                            loginHttp.login();
                            return;
                        }
                        if (jsonObject.getInt("status") == 200) {
                            ToastUtil.showShort(CQuerenshouhuobingpingjiaActivity.this, jsonObject.getString("message"));
                        } else {
                            ToastUtil.showShort(CQuerenshouhuobingpingjiaActivity.this, jsonObject.getString("message"));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
            }

            super.handleMessage(msg);
        }
    };


    public void submit() {

        QuerenDingdanHttp querenDingdanHttp =new QuerenDingdanHttp(this);
        querenDingdanHttp.uploadImg(taskId,imgs);
    }


}
